package com.quick.develop.framework.common.page;

import com.quick.develop.framework.common.constant.Constant;
import lombok.Data;

import java.io.Serializable;

/**
 * 响应信息主体
 *
 * @author YunTai
 */
@Data
public class R<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 成功
     */
    public static final int SUCCESS = Constant.SUCCESS;
    public static final String SUCCESS_TEXT = Constant.SUCCESS_TEXT;

    /**
     * 失败
     */
    public static final int FAIL = Constant.FAIL;

    private int code;

    private String msg;

    private T data;

    /**
     * @Description: 返回具体异常信息，便于调试使用
     * @Author: gql
     * @Date: 2022/4/22 9:55
     **/
    private String e;

    public static <T> R<T> ok() {
        return restResult(null, SUCCESS, SUCCESS_TEXT);
    }

    public static <T> R<T> ok(T data) {
        return restResult(data, SUCCESS, SUCCESS_TEXT);
    }

    public static <T> R<T> ok(T data, String msg) {
        return restResult(data, SUCCESS, msg);
    }

    public static <T> R<T> fail() {
        return restResult(null, FAIL, null);
    }

    public static <T> R<T> fail(String msg) {
        return restResult(null, FAIL, msg);
    }

    public static <T> R<T> fail(T data) {
        return restResult(data, FAIL, null);
    }

    public static <T> R<T> fail(T data, String msg) {
        return restResult(data, FAIL, msg);
    }

    public static <T> R<T> fail(int code, String msg) {
        return restResult(null, code, msg);
    }

    private static <T> R<T> restResult(T data, int code, String msg) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        return apiResult;
    }

    /**
     * @param data:
     * @param code:
     * @param msg:
     * @param e:
     * @Description: 返回结果，包含异常信息
     * @Author: gql
     * @Date: 2022/4/22 10:04
     * @return: com.quick.develop.framework.domain.R<T>
     **/
    public static <T> R<T> restResult(T data, int code, String msg, String e) {
        R<T> apiResult = new R<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        apiResult.setE(e);
        return apiResult;
    }

    /**
     * @param msg: 错误提示
     * @param e:   异常信息
     * @Description: 返回失败，包含异常提示和异常信息
     * @Author: gql
     * @Date: 2022/4/22 10:13
     **/
    public static <T> R<T> ex(String msg, String e) {
        return restResult(null, FAIL, msg, e);
    }

    /**
     * @param e: 异常信息
     * @Description: 返回异常信息，不包含异常提示
     * @Author: gql
     * @Date: 2022/4/22 10:36
     **/
    public static <T> R<T> ex(String e) {
        return restResult(null, FAIL, null, e);
    }
}
